package com.krtech.wecard.module.ramble.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.ramble.entity.*;
import com.krtech.wecard.module.ramble.enums.RambleEnums;
import com.krtech.wecard.module.ramble.service.*;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.enums.SysEnums;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/rambleDraw")
@Slf4j
@Api(tags = "佳得乐漫步-抽奖记录")
public class RambleDrawLogController {

    @Autowired
    private RambleDrawLogService rambleDrawLogService;

    @Autowired
    private SysKeyValueService keyValueService;

    @Autowired
    private RambleDrawChanceService chanceService;

    @Autowired
    private RambleAcceptService acceptService;

    @Autowired
    private RambleLeaderboardService leaderboardService;

    @Autowired
    private RambleLoginLogService loginLogService;

    @PostMapping("/getLogInfo")
    @ApiOperation(value = "获得历史领取信息")
    public RambleDrawLogVo getLogInfo(@RequestBody RambleDrawLog drawLog) {


        SysUser userInfo = LoginUtil.getUserInfo();

        if (userInfo == null || StrUtil.isEmpty(userInfo.getMipOpenId())){
            throw new BusinessException("当前用户异常，请联系管理员");
        }

        if (drawLog.getStepNumber() == null){
            throw new BusinessException("必填参数stepNumber不能为空");
        }

        //todo 这儿要加上之前的累积

        //总共领了多少张
        List<RambleAccept> totalAcceptList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone()));

        int prizeCount = rambleDrawLogService.getCanAcceptCount();
//        int prizeCount = 0;

        //计算已领数量
        int acceptCount = 0;
        if (totalAcceptList.size() > 0){
            acceptCount = totalAcceptList.size();
        }

        //获得抽奖次数
        RambleDrawChance drawChance = chanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, userInfo.getPhone()));


        drawLog.setCreatedTime(new Date());
        drawLog.setDrawTime(new Date());
        drawLog.setNickName(userInfo.getNickName());
        drawLog.setPhone(userInfo.getPhone());
        drawLog.setOpenId(userInfo.getMipOpenId());
        drawLog.setPrizeName("佳得乐优惠卷"+prizeCount + "张");
        drawLog.setFkPrizeId(prizeCount);

        RambleDrawLogVo drawLogVo = new RambleDrawLogVo();
        BeanUtil.copyProperties(drawLog,drawLogVo);
        drawLogVo.setDrawChance(drawChance);
        drawLogVo.setHasDrawCount(acceptCount);
        return drawLogVo;
    }

    @PostMapping("/getStepInfo")
    @ApiOperation(value = "获得当前步数的信息")
    public RambleDrawLogVo getStepInfo(@RequestBody RambleDrawLog drawLog) {


        SysUser userInfo = LoginUtil.getUserInfo();

        if (userInfo == null || StrUtil.isEmpty(userInfo.getMipOpenId())){
            throw new BusinessException("当前用户异常，请联系管理员");
        }

        if (drawLog.getStepNumber() == null){
            throw new BusinessException("必填参数stepNumber不能为空");
        }

        //todo 这儿要加上之前的累积

        //总共领了多少张
        List<RambleAccept> totalAcceptList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone()));

        int prizeCount = rambleDrawLogService.getCanAcceptCount();
//        int prizeCount = 0;

        //计算已领数量
        int acceptCount = 0;
        if (totalAcceptList.size() > 0){
            acceptCount = totalAcceptList.size();
        }

        //获得抽奖次数
        RambleDrawChance drawChance = chanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, userInfo.getPhone()));


        drawLog.setCreatedTime(new Date());
        drawLog.setDrawTime(new Date());
        drawLog.setNickName(userInfo.getNickName());
        drawLog.setPhone(userInfo.getPhone());
        drawLog.setOpenId(userInfo.getMipOpenId());
        drawLog.setPrizeName("佳得乐优惠卷"+prizeCount + "张");
        drawLog.setFkPrizeId(prizeCount);

        RambleDrawLogVo drawLogVo = new RambleDrawLogVo();
        BeanUtil.copyProperties(drawLog,drawLogVo);
        drawLogVo.setDrawChance(drawChance);
        drawLogVo.setHasDrawCount(acceptCount);
        return drawLogVo;
    }

    @PostMapping("/drawLog")
    @ApiOperation(value = "确定以当前步数来抽奖")
    public RambleDrawLogVo drawLog(@RequestBody RambleDrawLog drawLog) {
        //查询环境变量
        SysKeyValue stepKeyValue = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, RambleEnums.STEP));

        if (stepKeyValue == null){
            throw new BusinessException("环境变量step未配置，请联系管理员");
        }

        int step = Integer.parseInt(stepKeyValue.getSysValue());

        SysUser userInfo = LoginUtil.getUserInfo();

        if (userInfo == null || StrUtil.isEmpty(userInfo.getMipOpenId())){
            throw new BusinessException("当前用户异常，请联系管理员");
        }

        if (drawLog.getStepNumber() == null){
            throw new BusinessException("必填参数stepNumber不能为空");
        }

//        if (drawLog.getStepNumber() < step){
//            return null;
//        }

        //todo 这儿要加上之前的累积
        List<RambleDrawLog> totalDrawLog = rambleDrawLogService.list(new QueryWrapper<RambleDrawLog>()
                .eq(RambleDrawLog.COL_PHONE, userInfo.getPhone()));

        List<RambleAccept> hasAcceptList = acceptService.list(new QueryWrapper<RambleAccept>().eq(RambleAccept.COL_PHONE, userInfo.getPhone()));

        //领了几张
        int sumDrawLog = totalDrawLog.stream().mapToInt(RambleDrawLog::getFkPrizeId).sum();

        //已经领取掉的步数
        int  useStep = sumDrawLog * step;

        //总步数
        RambleLeaderboardVo leaderboardVo = leaderboardService.getData();
        Integer totalStep = leaderboardVo.getTotalStep();

        int canUseStep = totalStep - useStep;

        int prizeCount = 0;

        if (canUseStep > 0){
            prizeCount = canUseStep / step;
        }

        if (prizeCount <= 0){
            return null;
        }

        //获得抽奖次数
        RambleDrawChance drawChance = chanceService.getOne(new QueryWrapper<RambleDrawChance>().eq(RambleDrawChance.COL_PHONE, userInfo.getPhone()));


        drawLog.setCreatedTime(new Date());
        drawLog.setDrawTime(new Date());
        drawLog.setNickName(userInfo.getNickName());
        drawLog.setPhone(userInfo.getPhone());
        drawLog.setOpenId(userInfo.getMipOpenId());
        drawLog.setPrizeName("佳得乐优惠卷"+prizeCount + "张");
        drawLog.setFkPrizeId(prizeCount);
        drawLog.setLastStepNumber(totalStep);

//        drawLog.setLastStepNumber(drawLog.getStepNumber());
        rambleDrawLogService.save(drawLog);

        RambleDrawLogVo drawLogVo = new RambleDrawLogVo();
        BeanUtil.copyProperties(drawLog,drawLogVo);
        drawLogVo.setDrawChance(drawChance);
        if (hasAcceptList.size() > 0){
            drawLogVo.setHasDrawCount(hasAcceptList.size());
        }else {
            drawLogVo.setHasDrawCount(0);
        }
        return drawLogVo;
    }

    @PostMapping("/myDrawDownLog")
    @ApiOperation(value = "领取记录")
    public List<RambleAccept> myDrawDownLog(){
        SysUser userInfo = LoginUtil.getUserInfo();
        List<RambleAccept> rambleDrawLogList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone()));
        return rambleDrawLogList;
    }

}
